package com.bc.bczbweb.controller.Ajax;

import com.bc.bcapi.service.T_SequenceService;
import com.bc.commoncore.util.DateUtil;
import com.bc.commonentity.common.JsonResult;
import com.bc.bcapi.service.T_Role_infoService;
import com.bc.bcmodel.domain.T_Role_infoModel;
import com.bc.commonentity.enums.SeqNameEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.POST;
import java.math.BigInteger;

@RestController
@RequestMapping("ajax/t_role_info")
public class AjaxT_Role_infoController {
    @Autowired
    T_Role_infoService t_Role_infoService;

    @Autowired
    T_SequenceService t_SequenceService;

    @POST
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public JsonResult add(T_Role_infoModel req) {
        JsonResult js = new JsonResult();
        if (!req.getRole_id().equals("")) {
            T_Role_infoModel checkModel = new T_Role_infoModel();
            checkModel.setRole_name(req.getRole_name());
            long resultCount = t_Role_infoService.count(checkModel);
            if (resultCount >= 2) {
                //已存在
                js.setCode(2);
                return js;
            }
            req.setModify_time(DateUtil.getToday());
            t_Role_infoService.update(req);
        } else {
            T_Role_infoModel checkModel = new T_Role_infoModel();
            checkModel.setRole_name(req.getRole_name());
            long resultCount = t_Role_infoService.count(checkModel);
            if (resultCount >= 1) {
                //已存在
                js.setCode(2);
                return js;
            }
            req.setRole_id(t_SequenceService.genSequence(SeqNameEnum.MANAGE_ROLEINFO_SEQ));
            t_Role_infoService.insert(req);
        }
        return js;
    }

    @RequestMapping("del")
    public JsonResult del(String role_id) {
        JsonResult js = new JsonResult();
        T_Role_infoModel model = new T_Role_infoModel();
        model.setRole_id(role_id);
        T_Role_infoModel result = t_Role_infoService.findByNamedParam(model);

        model = new T_Role_infoModel();
        model.setRole_id(role_id);
        String status = "1";
        if (result.getStatus().equals("1")) {
            status = "0";
        }
        model.setStatus(status);
        t_Role_infoService.update(model);
        return js;
    }
}